<template>
	<view class="index_box">
		<!-- <view class="search_box" @click="toNewPage('/pages/search/search')">
			<view class="search dis_fx fx_column">
				<view class="icon"></view>
				<input type="text" placeholder="请输入您要搜索的目的地" placeholder-style="color: #D0D0D0">
			</view>
		</view> -->
		<view class="swiper">
			<swiper indicator-dots="true" autoplay="true" interval="3000" duration="500" indicator-color="#999999"
			 indicator-active-color="#FFCD00">
				<swiper-item v-for="(item ,index) in bannerArr"  :key="index">
					<view class="swiper-item uni-bg-red" @click="toOutPage(item.linkUrl)">
						<image :src="item.bannerUrl" class="coverImg"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="index_main" v-for="(productType,typeIndex) in productTypes">
			<div class="dis_fx tit_box fx_column"  @click="toList(typeIndex)">
				<view class="index_pub_tit dis_fx fx_column">
					<image src="../../static/tuzi_icon.png"></image>
					<view class="tit">{{productType.productTypeName || ""}} </view>
					<text>Package Tour</text>
				</view>
				<view class="more">更多>></view>
			</div>
			<view class="type_menu dis_fx">
				<view :class="destination.cur"  v-for="(destination,destinationIndex) in productType.destinations" @click="tabDestination(typeIndex,destinationIndex)">{{destination.destination}}</view>
			</view>
			<view class="pro_box" v-for="(destination,dindex) in productType.destinations" v-if="destination.cur">
				<view class="item" v-for="(item,index) in destination.products" v-if="index<4" @click="toNewPage('/pages/attractionsDetail/attractionsDetail?productId='+item.productId)">
					<view class="item_con">
						<view class="fz_0"><image :src="item.productMasterDiagram" mode="aspectFill"></image></view>
						<view class="text">
							<view class="addit">
								<text>{{item.productRendezvous || ""}}集合</text><view class="price">￥<text>{{item.minPrice || "0.00"}}</text>元起</view>
							</view>
							<view class="tit text-ellipsis-2">【{{item.productName || ""}}】 {{item.productSubtitle}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import util from '../../common/util.js'
	export default {
		data() {
			return {
				searchWord: '',
				bannerArr: [],
				targetAddress: [],
				hotTravel: [],
				code: '',
				searchProductForm: {
					rows: 10,
					page: 1,
					orderByClause: '',
					groupByClause: ''
				},
				productTypes: [],
				productIndex:0,
				destinationsIndex:0,
				reffent:""
			}
		},
		onLoad(option) {
			
			
			var that = this;
			if(option.reffent!=null){
				that.reffent=option.reffent;
				uni.setStorageSync('reffent', reffent); 
			}else{
				that.reffent =uni.getStorageSync('reffent'); 
			}
			console.log("推荐人："+that.reffent);
			
			var currentUrl = window.location.href;
			console.log("当前地址："+currentUrl.indexOf("uniapp"));
			if(currentUrl.indexOf("?uniapp")>-1){
				currentUrl = currentUrl.replace("#/","");
				currentUrl = currentUrl.replace("?uniapp","#");
				console.log(currentUrl);
				window.location.href = currentUrl;
			}
			
			
			var value = {}
			if(uni.getStorageSync('UserInfo')) {
				value =JSON.parse(uni.getStorageSync('UserInfo')); 
			}	
			function getQueryString(name)
			{
				 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
				 var r = window.location.search.substr(1).match(reg);
				 if(r!=null)return  unescape(r[2]); return null;
			}
			var code = getQueryString('code')	
			
			
			/* if(value!=null){
				if(!value.bindPhone){//每次检查是否绑定手机，没绑定 拦截去绑定
					that.toNewPage("/pages/bindMobile/bindMobile");
				}
			} */
			
			
			if(value==null||value.token==null||value.token==''||value.token=='undefined'){//登录状态不存在
				if(code!=null&&code.length>0){ //有code 用调用使用code 登录的接口 否则去微信授权获取code
					this.wxLogin(code)	
				}else{
					this.login();
				}
			}
			
			
			
		},
		onShow() {
			//this.getHomeProductByPage()
			this.getHomeInitData()
			var pages = getCurrentPages();
			console.log(pages)
		},
		methods: {
			//切换目的地
			tabDestination(typeIndex,destinationIndex){
				var that =this;
				typeIndex=parseInt(typeIndex)
				destinationIndex=parseInt(destinationIndex)
				var productTypes=that.productTypes;
				var productType=productTypes[typeIndex];
				for(var i in productType.destinations){
					if(i==destinationIndex){
						productType.destinations[i].cur="cur"
					}else{
						productType.destinations[i].cur=""
					}
				}
				productTypes[typeIndex]=productType;
				that.productTypes=[];
				that.productTypes=productTypes;
			},
			toNewPage(url) {
				uni.navigateTo({url});
			},
			toOutPage(url) {
				window.location.href=url;
			},
			
			toList(typeIndex) {
				var that=this;
				var productType=that.productTypes[typeIndex];
				var destinationsIndex=0;
				for(var i in productType.destinations){
					if(productType.destinations[i].cur=="cur"){
						destinationsIndex=i;
					}
				}
				var url='/pages/list/list?productIndex='+typeIndex+"&destinationsIndex="+destinationsIndex;
				uni.navigateTo({url});
			},
			getHomeProductByPage() {
				
				var _self = this
				uni.showLoading({
					mask:true,
					title: '加载中'
				});
				util.fetchPost('/api/index/productList', this.searchProductForm, function(res){					
					_self.hotTravel = res.data
					_self.hotTravel.forEach(x=>{
						if(x.productMasterDiagram) {
							x.productMasterDiagram = x.productMasterDiagram.split(',')[0]
						}
					})
					uni.hideLoading();									
				},function(err){
					
				},false)
			},
			getHomeInitData () {
				var _self = this
				uni.showLoading({
					mask:true,
					title: '加载中'
				});
				util.fetchGet('/api/index/appIndex', {}, function(res){					
					_self.bannerArr = res.data.banners
					var productTypes=res.data.productTypes;
					for(var i in res.data.productTypes){
						if(productTypes[i].destinations.length>0){
							productTypes[i].destinations[0].cur="cur"
						}
					}
					_self.productTypes = productTypes
					uni.hideLoading();
				},function(err){
					
				},false)				
			},
			login() {
				// #ifdef H5
				var  lastUrl = window.location.href
				uni.setStorageSync('lastUrl', lastUrl); //记录下最后url 返回时，由首页做中转
				location.href = util.serviceTarget+'/api/wx_authorize.do?callback='+util.webTarget
				// #endif
			},
			wxLogin (code) {
				var that =this;
				var params={
					code:code
				}
				if(that.reffent){
					params={
						code:code,
						reffent:that.reffent
					}
				}
				uni.showLoading({
					mask:true,
					title: '加载中'
				});
				util.fetchPost('/api/wx_login.do', params, function(res){
					
					var lastUrl = uni.getStorageSync('lastUrl');
				    console.log("授权前地址："+lastUrl);
					
					if(!res || !res.data || !res.data.token){
						uni.removeStorageSync("UserInfo")
					}else{
						uni.removeStorageSync("UserInfo");
						// 存储登录的数据集
						uni.setStorageSync('UserInfo', JSON.stringify(res.data))
						let storageTime=new Date().getTime();
						uni.setStorageSync("storageTime",storageTime);
						
						//uni.setStorageSync('lastUrl', lastUrl); //记录下最后url 返回时，由首页做中转
					
						console.log(res.data);
						
						if(lastUrl.length>0){
								//that.toNewPage(lastUrl);
								window.location.href = lastUrl;
						}else if(res.data.bindPhone==null||res.data.bindPhone==''){
							that.toNewPage("/pages/bindMobile/bindMobile")
						}
						
						
						/* uni.navigateBack({
							delta: 1
						}); */
					}
					uni.hideLoading();
				},function(err){
					
				},false)
			}
  		}
	}
</script>

<style>
	.index_box{
		padding-bottom: 100upx;
	}
	.pro_box{
		padding: 80upx 10upx 0;
		font-size: 0;
	}
	.pro_box .item{
		display: inline-block;
		vertical-align: top;
		width: 50%;
		padding: 0 10upx;
		margin-bottom: 40upx;
	}
	.pro_box .item_con{
		box-shadow: 0 0 5upx 1upx #F0F0F0;
		border-radius: 10upx;
	}
	.pro_box .item image{
		width: 324upx;
		height: 180upx;
		border-radius: 10upx;
		box-shadow: 0 5upx 5upx 0upx #CECCCD;
	}
	.pro_box .item .text{
		padding: 10upx;
		border-radius:10upx;
	}
	.pro_box .item .tit{
		font-size: 22upx;
		color: #313131;
	}
	.pro_box .item .addit{
		font-size: 18upx;
		color: #585858;
		display: flex;
		justify-content: space-between;
		margin: 10upx 0;
	}
	.pro_box .item .addit .price{
		color: #fc5c1e;
		display: flex;
		align-items: center;
	}
	.pro_box .item .addit .price text{
		font-size: 28upx;
		font-family: 'priceFont';
	}
	.type_menu{
		padding: 0 60upx;
	}
	.type_menu>view{
		margin: 0 7upx;
		border: 1px solid #070002;
		flex: 1;
		text-align: center;
		height: 44upx;
		line-height: 42upx;
	}
	.type_menu>view.cur{
		background: #313131;
		color: #ffc801;
	}
	.tit_box{
		padding: 0 60upx;
		justify-content: space-between;
	}
	.index_pub_tit{
		padding: 60upx 0;
	}
	.index_pub_tit image{
		width: 21upx;
		height: 48upx;
	}
	.index_pub_tit .tit{
		font-size: 32upx;
		color: #313131;
		font-weight: 600;
		margin: 0 10upx 0 25upx;
	}
	.index_pub_tit text{
		font-size: 27upx;
		color: #313131;
		font-weight: normal;
	}
	.tit_box .more{
		font-size: 25upx;
		color: #fd5c1e;
	}
	.search_box {
		background: #fff;
		padding: 20upx 24upx;
	}

	.search_box .search {
		width: 100%;
		height: 70upx;
		border-radius: 35upx;
		background: #F1F1F1;
		padding: 0 30upx;
	}

	.search_box .search input {
		font-size: 28upx;
		flex: 1;
	}

	.search_box .search .icon {
		background: url() no-repeat center/contain;
		width: 25upx;
		height: 30upx;
		margin-right: 20upx;
	}
	
	.index_box .swiper {
		width: 100%;
		box-sizing: border-box;
		padding: 30upx;
	}
	.index_box .swiper swiper{
		border-radius: 20upx;
		width: 100%;
		height: 354upx;
		box-shadow: 0 0 10upx 2upx rgba(0,0,0,0.2);
	}
	.index_box swiper .swiper-item{
		width: 100%;
		height: 100%;
	}
	.index_box swiper image{
		border-radius: 20upx;
	}
	.index_main {
		padding: 0 23upx;
	}

	.index_main .block1_con .item {
		width: 220upx;
		margin-right: 10upx;
		display: inline-block;
		vertical-align: top;
		margin-bottom: 20upx;
	}

	.index_main .block1_con .item.third_item {
		margin-right: 0;
	}

	.index_main .block1_con .item .img_box image {
		width: 220upx;
		height: 220upx;
		border-radius: 14upx;
	}

	.index_main .block1_con .item .img_box .tag {
		background: url() no-repeat center/cover;
		width: 94upx;
		height: 31upx;
		position: absolute;
		top: 0;
		left: 0;
	}

	.index_main .block1_con .item .img_box .tag2 {
		background-image: url();
	}

	.index_main .block1_con .item .img_box .tag3 {
		background-image: url();
	}

	.index_main .block1_con .item .text {
		margin-top: 10upx;
		font-size: 30upx;
		color: #434443;
		line-height: 1.5;
		text-align: center;
		
	}

	.index_main .block1_con .item .text .info {
		font-size: 22upx;
		color: #C1C1C1;
		line-height: 1.5;
	}
	
	.index_main .block2_con{
		padding-bottom: 100upx
	}
	
	.index_main .block2_con .item {
		box-shadow: 0 0 18px 0px rgba(106, 79, 13, 0.12);
		margin-bottom: 20upx;
		padding-bottom: 40upx;
	}

	.index_main .block2_con .item image {
		width: 100%;
		height: 320upx;
		border-radius: 5px 5px 0 0;
	}

	.index_main .block2_con .item .tit {
		padding: 35upx 24upx;
	}

	.index_main .block2_con .item .tit text {
		color: #333333;
		font-size: 28upx;
	}

	.index_main .block2_con .item .tit .type {
		background: #FC5C1D;
		color: #fff;
		width: 140upx;
		height: 46upx;
		border-radius: 23upx;
		font-size: 24upx;
		text-align: center;
		line-height: 46upx;
		margin-right: 20upx;
	}

	.index_main .block2_con .item .price {
		font-size: 36upx;
		color: #FFC800;
		padding: 0 34upx;
	}
</style>
